<?php 

if (!defined('PUBLIC_HTML'))
{
	exit;
}

$submit		= (isset($_POST['submit']))		? true : false;
$redirect	= (isset($_POST['to_page']))	? $_POST['to_page'] : 'home';
$action		= ($user->logged_in)			? 'logout' : 'login';

// Pre-define post values
$username	= (isset($_POST['username']))	? $_POST['username']	: '';
$password	= (isset($_POST['password']))	? $_POST['password']	: '';
$remember	= (isset($_POST['remember']))	? true					: false;
if ($remember)
{
	$checked	= ($_POST['remember'] == 'remember')	? ' checked="checked"' : '';
	$notime		= ($_POST['remember'] == 'remember')	? true : false;
}
else
{
	$notime		= false;
	$checked	= '';
}

switch ($action)
{
	case 'logout':
		$user->logout();
	break;

	case 'login':
	
		if($submit)
		{
			$time = ($notime) ? 36000000 : 3600;
			$login = $user->login($_POST['username'], $string->secure($_POST['password'], 'password'), $redirect, $time);
			redirect('cms.php');
			exit;
		}
		
		if (!$submit || !$login)
		{			
			$tpl_content = new TemplatePower(PATH_HTML . 'login_body' . EXT_HTML);
			$tpl_content->prepare();
			
			$meta[] = add_meta_tags('robots', 'noindex,nofollow');
			
			$tpl_content_vars = array(
				'PAGE_TITLE'	=> $page_title,
				'PAGE_CONTENT'	=> $page_content,
				
				'USERNAME'		=> $username,
				'PASSWORD'		=> $password,
				'CHECKED'		=> $checked
			);
			
			$tpl_content->assign($tpl_content_vars);
			//$tpl_content->printToScreen();
		}
	break;	
	
	default:
		throw new Exception('Invalid session value given');
	break;
}

?>